package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.PostMapper;
import com.ruoyi.system.service.PostService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, SysPost> implements PostService {
    @Override
    public IPage<SysPost> selectPostList(Page<SysPost> page, SysPost post) {
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(post.getPostCode()),SysPost::getPostCode,post.getPostCode())
                .eq(StringUtils.isNotBlank(post.getStatus()),SysPost::getStatus,post.getStatus())
                .like(StringUtils.isNotBlank(post.getPostName()),SysPost::getPostName,post.getPostName());
        return baseMapper.selectPage(page,wrapper);
    }

    @Override
    public boolean checkPostNameUnique(SysPost post) {
        Long postId = com.ruoyi.common.core.utils.StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost sysPost = baseMapper.selectOne(Wrappers.<SysPost>lambdaQuery().eq(SysPost::getPostName,post.getPostName()));
        if (com.ruoyi.common.core.utils.StringUtils.isNotNull(sysPost) && sysPost.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        Long postId = com.ruoyi.common.core.utils.StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost sysPost = baseMapper.selectOne(Wrappers.<SysPost>lambdaQuery().eq(SysPost::getPostCode,post.getPostCode()));
        if (com.ruoyi.common.core.utils.StringUtils.isNotNull(sysPost) && sysPost.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return baseMapper.selectPostListByUserId(userId);
    }
}
